/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using VSLangProj80;
using EnvDTE;
using VSLangProj;
using DaveSexton.DocProject.Resources;

namespace DaveSexton.DocProject
{
	public class VSProjectOutput : MarshalByRefObject, IProjectOutput
	{
		#region Public Properties
		public virtual string RelativeOutputPath
		{
			get
			{
				string path = project.ConfigurationProperties["OutputPath"];

				if (string.IsNullOrEmpty(path))
					throw new InvalidOperationException(Errors.NoOutputPath);
				else if (Path.IsPathRooted(path))
				{
					Uri root = new Uri(project.Directory);

					return PathFormat.MakeLocal(System.Web.HttpUtility.UrlDecode(
						root.MakeRelativeUri(new Uri(path)).ToString()));
				}
				else
					return PathFormat.MakeLocal(path);
			}
		}

		public string OutputPath
		{
			get
			{
				string path = RelativeOutputPath;

				if (Path.IsPathRooted(path))
					return path;
				else
					return Path.Combine(project.Directory, path);
			}
		}

		public virtual string OutputFileName
		{
			get
			{
				string name = project.Properties["OutputFileName"];

				return (string.IsNullOrEmpty(name)) ? null : name;
			}
		}

		public virtual SystemPath OutputFile
		{
			get
			{
				return new SystemPath(Path.Combine(OutputPath, OutputFileName), false, false);
			}
		}

		public virtual SystemPath XmlDocumentationFile
		{
			get
			{
				string file = project.ConfigurationProperties["DocumentationFile"];

				if (string.IsNullOrEmpty(file))
					return SystemPath.Empty;
				else if (Path.IsPathRooted(file))
					return new SystemPath(file, false, false);
				else if (project.IsVisualBasic)
					// comments file for a Visual Basic project just contains the name
					return new SystemPath(OutputPath, file, false, false);
				else
					// comments file for Visual C# and Visual J# projects contain the relative path from the project file.  e.g., bin\Debug\comments.xml
					return new SystemPath(project.Directory, file, false, false);
			}
		}

		public IAnyProject Project
		{
			get
			{
				return project;
			}
		}
		#endregion

		#region Private / Protected
		private readonly VSAnyProject project;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="VSProjectOutput" /> class.
		/// </summary>
		public VSProjectOutput(VSAnyProject project)
		{
			if (project == null)
				throw new ArgumentNullException("project");

			this.project = project;
		}
		#endregion

		#region Methods
		[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.LinkDemand,
			Flags = System.Security.Permissions.SecurityPermissionFlag.Infrastructure)]
		public override object InitializeLifetimeService()
		{
			return null;
		}

		public override string ToString()
		{
			return "{" + OutputFileName + ", " + XmlDocumentationFile.Path + "}";
		}
		#endregion
	}
}
